<?php
include_once(dirname(__FILE__)."/../inc/common.php"); 
$method = isset($_GET["method"])?$_GET["method"]:"";
$isCopyFlag = isset($_GET["isCopyFlag"])?$_GET["isCopyFlag"]:"";
$oid = isset($_GET["oid"])?$_GET["oid"]:"";
$subjectId = isset($_GET["subjectId"])?$_GET["subjectId"]:"";

if($method=="add" || ($method=="edit" && $isCopyFlag && $oid)){
	$tallyAccountClass = new tally_account();
	$accountList = $tallyAccountClass->findAccountList($userId);
	$tallyFlowTypeClass = new tally_flowType();
	$flowTypeList = $tallyFlowTypeClass->findFlowTypeByInout($userId, tally_flow::$INOUT_INCOMME);
	
	$oldFlow = array();
	$tags = "";
	$tagIds = "";
	$tradeTime = time();
	$money = "";
	$desc = "";
	if($method=="edit" && $isCopyFlag && $oid){
		$tallyTagClass = new tally_tag();
		$tagListTemp = $tallyTagClass->findTagByAccountId($userId, null, true, 0, -1);
		$tagList = array();
		foreach($tagListTemp as $tag){
			$tagList[$tag["id"]] = $tag;
		}
		$flowClass = new tally_flow();
		$oldFlow = $flowClass->findFlowById ( $userId, $oid );
		$money = $oldFlow["moneyIn"];
		$desc = $oldFlow["desc"];
		$tradeTime = strtotime($oldFlow["tradeTime"]);
		$tagIdList = $flowClass->findTagByFlow($userId, $oldFlow["id"]);
		if($tagIdList){
			foreach($tagIdList as $tagMap){
				if($tags){
					$tags .= ",";
					$tagIds .= ",";
				}
				$tags .= $tagList[$tagMap["tagId"]]["name"];
				$tagIds .= $tagMap["tagId"];
			}
		}
	}
?>
<div id="winwrapper" class="winwrapper">
<div>
  	<form id="addForm" name="addForm"  method="post">
  			
		<div class="content">
		    <fieldset>
		    	<legend>基本信息</legend>
		    		<table cellpadding="0" cellspacing="1" class="form_table">
		    			<tr>
							<td class="caption">记一笔:
							</td>
							<td class="field">
								<span>收入</span>
								<input class="text" type="text" name="inputActivity" maxlength="20" onblur="Tally.showActivity(this);" style="display: none;">
								<input class="text" type="hidden" name="activity" maxlength="20" value="收入"/>
								<input class="text" type="hidden" name="bizType" maxlength="8" value="11"/>
							</td>
							
							<td><label for="tagName">标签:</label></td>
      						<td class="field">
      							<input type="text" class="text rd" readonly="readonly" id="tagName" maxlength="64" name="tags" value="<?php echo $tags?>">
      							<input type="hidden" name="tagIds" value="<?php echo $tagIds?>">
      							<a title="选择标签" href="#" onclick="TallyTagPicker.open(this,'1');return false;"><img src="../images/icons/select.gif" align="absmiddle"/></a>
							</td>
						</tr>
						<tr>
							
							<td class="caption"><label for="tradeTime" ><span class="em">*</span>日期:</label></td>
							<td class="field"><INPUT class="Wdate date" type="text"  id="tradeTime" name="tradeTime"  value="<?php echo date("Y-m-d H:i", $tradeTime)?>"  notnull="true"  onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',isShowClear:false});"/></td>
							
							<td class="caption"><label><span class="em">*</span>账户:</label></td>
      						<td class="field">
      						<select class="m" style="width: 100px;" name="subjectId0"  id="subjectId0" notnull="true" onchange="Account.goAdd(this,{'isPay':1});"><!-- {'isPay':1,'accounutType':0} -->
      						<?php
							foreach($accountList as $account){
							?>
								<option value="<?php echo $account["id"]?>" <?php echo ($isCopyFlag && $account["id"]==$oldFlow["accountId"] || !$isCopyFlag && $account["isDefault"] || $account["id"]==$subjectId)?"selected":""?>>
									<?php echo $account["name"]?>
								</option>	
      						<?php
							}
							?>
      						</select>
							
							</td>
							
						</tr>
						<tr>
							<td class="caption">
								<label for="subjectId1"><span class="em">*</span>收入类别:</label>
							</td>
       						<td class="field">
      	    					<select class="m" name="subjectId1"  id="subjectId1"  notnull="true" onchange="categroyChange=3;Category.goAdd(this,{'subjectCate':3});">
      	    							<?php
										foreach($flowTypeList as $flowType){
										?>
											<option value="<?php echo $flowType["id"]?>" <?php echo ($isCopyFlag && $flowType["id"]==$oldFlow["flowType"] || !$isCopyFlag && $flowType["isDefault"])?"selected":""?>>
												<?php echo $flowType["name"]?>
											</option>	
			      						<?php
										}
										?>   	
      							</select>
      						</td> 
      	
      						<td class="caption"><label for="amount"><span class="em">*</span>金额:</label></td>
      						<td class="field"><input type="text" class="number" maxlength="15" notnull="true"  datatype="number" integerlength="12" id="amount" name="amount" value="<?php echo $money?>"/>
								<a style="cursor: pointer;"><img src="../images/icons/calculator.gif" title="计算器" align="absmiddle" onclick="Calc.open(this);"/></a>
      						</td>
						</tr>
						<tr>
							<td class="caption"><label for="remark">说明:</label></td>
							<td class="field" colspan="3">
      							<textarea rows="2" cols="10" maxlength="1512" name="remark" id="remark"><?php echo $desc?></textarea>
							</td>
						</tr>
				</table>
				
			</fieldset>
		</div>
			
			<div class="buttons">
			    <input id="saveBtn" type="button" class="button"  value="保存" onclick="Income.doAdd(this);">
			    <input id="saveConBtn" type="button" class="button"  value="保存并继续" onclick="Income.doAdd(this);">
			</div>
			
			<div class="tip_wrapper" style="display:none">
				<a class="colse_btn" onclick="Help.goClose();" href="#" align="absmiddle"><img src="images/icons/icon_close.gif"/></a>
				<span class="title">提示:</span>
				<span class="tip_content"></span>
			</div>
  	
 </form>
</div>
</div>
<?php
}else if($method=="edit"){
	$oid = isset($_GET["oid"])?$_GET["oid"]:0;
	$tallyAccountClass = new tally_account();
	$accountList = $tallyAccountClass->findAccountList($userId);
	$tallyFlowTypeClass = new tally_flowType();
	$flowTypeList = $tallyFlowTypeClass->findFlowTypeByInout($userId, tally_flow::$INOUT_INCOMME);
	
	$flowClass = new tally_flow();
	$flow = $flowClass->findFlowById ( $userId, $oid );
	$tradeTime = strtotime($flow["tradeTime"]);
	$tallyTagClass = new tally_tag();
	$tagListTemp = $tallyTagClass->findTagByAccountId($userId, null, true, 0, -1);
	$tagList = array();
	foreach($tagListTemp as $tag){
		$tagList[$tag["id"]] = $tag;
	}
	$tags = "";
	$tagIds = "";
	$tagIdList = $flowClass->findTagByFlow($userId, $flow["id"]);
	if($tagIdList){
		foreach($tagIdList as $tagMap){
			if($tags){
				$tags .= ",";
				$tagIds .= ",";
			}
			$tags .= $tagList[$tagMap["tagId"]]["name"];
			$tagIds .= $tagMap["tagId"];
		}
	}
	?>
<div id="winwrapper" class="winwrapper">


<form id="editForm" name="editForm"  method="post">
<input type="hidden" name="voucherId" id="voucherId" value="<?php echo $oid?>">
	<div class="content">
		    <fieldset>	 	
		    	<legend>基本信息</legend>
		    		<table cellpadding="0" cellspacing="1" class="form_table">
		    			<tr>
							<td class="caption">修改一笔:
							</td>
							<td class="field">
								<span>收入</span>
								<input class="text" type="hidden" name="bizType" maxlength="8" value="11"/>
							</td>
							<td class="caption"><label>标签:</label></td>
      						<td class="field">
      							<input type="text" class="text rd" readonly="readonly" id="tagName" name="tags" value="<?php echo $tags?>">
      							<input type="hidden" name="tagIds" value="<?php echo $tagIds?>">
      							<a title="选择标签" href="#" onclick="TallyTagPicker.open(this,'1');return false;"><img src="../images/icons/select.gif" align="absmiddle"/></a>
      						</td>
						</tr>
						<tr>
							<td class="caption"><label for="tradeTime" ><span class="em">*</span>日期:</label></td>
							<td class="field"><INPUT class="Wdate date" type="text"  id="tradeTime" name="tradeTime"  value="<?php echo date("Y-m-d H:i", $tradeTime)?>"  notnull="true"  onFocus="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm',isShowClear:false});"/></td>
							<td class="caption"><label><span class="em">*</span>账户:</label></td>
      						<td class="field">
      						<select class="m" style="width: 100px;" name="subjectId0"  id="subjectId0" notnull="true" onchange="Account.goAdd(this,{'isPay':1});">
 				 			<?php
							foreach($accountList as $account){
							?>
								<option value="<?php echo $account["id"]?>" <?php echo $flow["accountId"]==$account["id"]?"selected":""?>>
									<?php echo $account["name"]?>
								</option>	
      						<?php
							}
							?>
      						</select>
							
							</td>
						</tr>
						<tr>
							<td class="caption">
								<label for="subjectId1"><span class="em">*</span>收入类别:</label>
							</td>
       						<td class="field">
      	    					<select class="m" name="subjectId1"  id="subjectId1"  notnull="true" onchange="categroyChange=3;Category.goAdd(this,{'subjectCate':3});">
      	    							<?php
										foreach($flowTypeList as $flowType){
										?>
											<option value="<?php echo $flowType["id"]?>" <?php echo $flow["flowType"]==$flowType["id"]?"selected":""?>>
												<?php echo $flowType["name"]?>
											</option>	
			      						<?php
										}
										?>
      							</select>
      						</td> 
      	
      						<td class="caption"><label for="amount"><span class="em">*</span>金额:</label></td>
      						<td class="field"><input type="text" class="number" maxlength="15" notnull="true"  datatype="number" id="amount" name="amount" value="<?php echo $flow["moneyIn"]?>"/>
								<a style="cursor: pointer;"><img src="../images/icons/calculator.gif" title="计算器" align="absmiddle" onclick="Calc.open(this);"/></a>
      						</td>
							
						</tr>
						<tr>
							<td class="caption"><label for="remark">说明:</label></td>
							<td class="field" colspan="3">
									<textarea rows="2" cols="10" maxlength="1512" name="remark" id="remark"><?php echo $flow["desc"]?></textarea>
							</td>
						</tr>
				</table>
			</fieldset>
		</div>
			
			<div class="buttons">
				
				
					<input id="saveBtn" type="button" class="button"  value="确定" onclick="Income.doEdit(this);"/>
		        	<input type="button" class="button"  value="取消" onclick="CM.close(this);return false;"/>
				
			    
			</div>
			
			<div class="tip_wrapper" style="display:none">
				<a class="colse_btn" onclick="Help.goClose();" href="#" align="absmiddle"><img src="../images/icons/icon_close.gif"/></a>
				<span class="title">提示:</span>
				<span class="tip_content"></span>
			</div>
</form>
</div>
	<?php
}else if($method=="doAdd"){
	if(isset($_POST["tradeTime"]) && $_POST["tradeTime"] 
			&& isset($_POST["amount"]) && $_POST["amount"] 
			&& isset($_POST["subjectId0"]) && $_POST["subjectId0"] 
			&& isset($_POST["subjectId1"]) && $_POST["subjectId1"]){
		try{
			$amount = $_POST["amount"]+0;
			$tradeTime = date("Y-m-d H:i:s", strtotime($_POST["tradeTime"]));
			$tags = isset($_POST["tagIds"])?$_POST["tagIds"]:"";
			$desc = isset($_POST["remark"])?$_POST["remark"]:"";
			$flowClass = new tally_flow();
			$flowClass->createFlow($userId, intval($_POST["subjectId0"]), $tradeTime, tally_flow::$INOUT_INCOMME, 
				intval($_POST["subjectId1"]), $amount, 0, explode(",", $tags), $desc);
			$result = array(
				"returnCode" => 0,
				"returnMsg" => "新增成功",
			);
		}catch(Exception $e){
			$result = array(
				"returnCode" => 2,
				"returnMsg" => $e->getMessage()?$e->getMessage():"新增失败",
			);
		}
	}else{
		$result = array(
			"returnCode" => 1,
			"returnMsg" => "参数错误",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}else if($method=="doEdit"){
	if(isset($_POST["voucherId"]) && $_POST["voucherId"] 
			&& isset($_POST["tradeTime"]) && $_POST["tradeTime"]
			&& isset($_POST["amount"]) && $_POST["amount"]
			&& isset($_POST["subjectId0"]) && $_POST["subjectId0"]
			&& isset($_POST["subjectId1"]) && $_POST["subjectId1"]){
		try{
			$amount = $_POST["amount"]+0;
			$tradeTime = date("Y-m-d H:i:s", strtotime($_POST["tradeTime"]));
			$tagIds = isset($_POST["tagIds"])?$_POST["tagIds"]:"";
			$desc = isset($_POST["remark"])?$_POST["remark"]:"";
			$flowClass = new tally_flow();
			$flow = $flowClass->findFlowById($userId, $_POST["voucherId"]);
			if($flow){
				$flow["tradeTime"] = $tradeTime;
				$flow["accountId"] = $_POST["subjectId0"];
				$flow["flowType"] = $_POST["subjectId1"];
				$flow["moneyIn"] = $amount;
				$flow["moneyOut"] = 0;
				$flow["desc"] = $desc;
				$flowClass->updateFlowInfo($userId, $_POST["voucherId"], $flow, explode(",", $tagIds));
				$result = array(
					"returnCode" => 0,
					"returnMsg" => "修改成功",
				);
			}else{
				throw new flowException("此记录不存在");
			}
		}catch(Exception $e){
			$result = array(
				"returnCode" => 2,
				"returnMsg" => $e->getMessage()?$e->getMessage():"修改失败",
			);
		}
	}else{
		$result = array(
			"returnCode" => 1,
			"returnMsg" => "参数错误",
		);
	}
	$jsonHandle = new ext_jsonHandle();
	$jsonHandle->jsonOut($result);
	exit();
}
?>